package com.yxhl.zoume.core.officialcar.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBarForCar;
import com.yxhl.zoume.core.specialcar.model.CarLocationMapEntrance;
import com.yxhl.zoume.core.car.fragment.common.SelectCityFragment;
import com.yxhl.zoume.core.officialcar.fragment.OfficialCarLocationMapFragment;
import com.yxhl.zoume.core.officialcar.fragment.OfficialCarPOISearchFragment;
import com.yxhl.zoume.core.officialcar.fragment.RentHourlyOfficialCarFragment;
import com.yxhl.zoume.core.officialcar.fragment.SingleWayOfficialCarFragment;
import com.yxhl.zoume.utils.SoftInputUtils;

import java.util.List;

import butterknife.BindView;

/**
 * created by sugarya 16/08/16
 * Fragment容器,Fragment管理者
 */
public class OfficialCarContainerActivity extends BaseActivityWithToolBarForCar {

    public static final String FRAGMENT_SINGLE_WAY = "SingleWayOfficialCarFragment";
    public static final String FRAGMENT_RENT_HOURLY = "RentHourlyOfficialCarFragment";
    public static final String FRAGMENT_CAR_LOCATION_MAP = "OfficialCarLocationMapFragment";
    public static final String FRAGMENT_CAR_POI_SEARCH = "CarPOISearchFragment";
    public static final String FRAGMENT_SELECT_CITY = "selectCityFragment";
    private static final String TAG = "OfficialCarContainerAct";
    private static final String YI_YANG_CITY = "益阳市";
    //widgets
    @BindView(R.id.frame_official_car_content)
    FrameLayout mOfficialCarContentFrame;


    //arguments
    /**
     * 用户选择的城市
     */
    private String mUserSelectCity = "未选中";


    private String mCurrentShowFragmentTab;

    private String mLastShowFragmentTab;

    private CarLocationMapEntrance mEntrance;

    public static Intent getCallingIntent(Activity activity) {
        Intent intent = new Intent(activity, OfficialCarContainerActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeShow();
    }

    private void initializeShow() {
        displayOneFragment(FRAGMENT_SINGLE_WAY, CarLocationMapEntrance.DEFAULT);
    }


    /**
     * 显示特定Tag的Fragment,如果是第一次显示,则新建并添加该Fragment
     *
     * @param fragmentTab
     * @param entrance
     */
    public void displayOneFragment(String fragmentTab, CarLocationMapEntrance entrance) {
        mEntrance = entrance;
        mCurrentShowFragmentTab = fragmentTab;

        FragmentManager manager = getSupportFragmentManager();
        if (manager != null) {
            FragmentTransaction transaction = manager.beginTransaction();

            //设置过渡动画
            if (!TextUtils.isEmpty(mLastShowFragmentTab)) {
                if (!mLastShowFragmentTab.equals(fragmentTab)) {
                    switch (mLastShowFragmentTab) {
                        case FRAGMENT_RENT_HOURLY:
                        case FRAGMENT_SINGLE_WAY:
                            switch (fragmentTab) {
                                case FRAGMENT_RENT_HOURLY:
                                    transaction.setCustomAnimations(R.anim.right_enter, R.anim.left_out, 0, 0);
                                    break;
                                case FRAGMENT_SINGLE_WAY:
                                    transaction.setCustomAnimations(R.anim.left_enter, R.anim.right_out, 0, 0);
                                    break;
                            }
                            break;
                    }
                }
            }

            List<Fragment> fragments = manager.getFragments();
            if (fragments != null && fragments.size() > 0) {
                for (Fragment f : fragments) {
                    if (f != null) {
                        transaction.hide(f);
                    }
                }
            }

            Fragment fragmentByTag = manager.findFragmentByTag(fragmentTab);
            if (fragmentByTag != null) {
                if (fragmentByTag instanceof OfficialCarLocationMapFragment) {
                    OfficialCarLocationMapFragment locationMapFragment = (OfficialCarLocationMapFragment) fragmentByTag;
                    locationMapFragment.setEntrance(entrance);
                } else if (fragmentByTag instanceof OfficialCarPOISearchFragment) {
                    OfficialCarPOISearchFragment poiSearchFragment = (OfficialCarPOISearchFragment) fragmentByTag;
                    poiSearchFragment.setEntrance(entrance);
                } else if (fragmentByTag instanceof SelectCityFragment) {
                    SelectCityFragment selectCityFragment = (SelectCityFragment) fragmentByTag;
                    selectCityFragment.setEntrance(entrance);
                }
                transaction.show(fragmentByTag);
            } else {
                transaction.add(R.id.frame_official_car_content, fragmentProvider(fragmentTab, entrance), fragmentTab);
            }

            transaction.commitAllowingStateLoss();

            mLastShowFragmentTab = fragmentTab;
        }
    }

    /**
     * Fragment提供者
     *
     * @param fragmentTag
     * @param entrance
     * @return
     */
    private Fragment fragmentProvider(String fragmentTag, CarLocationMapEntrance entrance) {

        switch (fragmentTag) {
            case FRAGMENT_SINGLE_WAY:
                return SingleWayOfficialCarFragment.newInstance();

            case FRAGMENT_RENT_HOURLY:
                return RentHourlyOfficialCarFragment.newInstance();

            case FRAGMENT_CAR_LOCATION_MAP:
                if (entrance != null) {
                    switch (entrance) {
                        case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
                            return OfficialCarLocationMapFragment.newInstance(CarLocationMapEntrance.OFFICIAL_CAR_SINGLE_WAY_GET_IN);
                        case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
                            return OfficialCarLocationMapFragment.newInstance(CarLocationMapEntrance.OFFICIAL_CAR_SINGLE_WAY_GET_OFF);
                        case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                            return OfficialCarLocationMapFragment.newInstance(CarLocationMapEntrance.OFFICIAL_CAR_RENT_HOURLY_GET_IN);
                        default:
                            return OfficialCarLocationMapFragment.newInstance(CarLocationMapEntrance.OFFICIAL_CAR_SINGLE_WAY_GET_IN);
                    }
                }

            case FRAGMENT_CAR_POI_SEARCH:
                return OfficialCarPOISearchFragment.newInstance(entrance);
            case FRAGMENT_SELECT_CITY:
                return SelectCityFragment.newInstance(entrance);

            default:
                return SingleWayOfficialCarFragment.newInstance();
        }

    }

    /**
     * 返回键显示特定Tag的Fragment
     *
     * @param fragmentTab
     */
    public void showOneFragmentOnBackPressed(String fragmentTab) {
        FragmentManager manager = getSupportFragmentManager();
        if (manager != null) {
            List<Fragment> fragments = manager.getFragments();
            FragmentTransaction transaction = manager.beginTransaction();
            for (Fragment f : fragments) {
                if (f.getClass().getSimpleName().equals(fragmentTab)) {
                    transaction.show(f);
                } else {
                    transaction.hide(f);
                }
            }
            transaction.commitAllowingStateLoss();
        }
    }

    @Override
    public void onBackPressed() {
        SoftInputUtils.closeInputKeyboard(this);
        switch (mCurrentShowFragmentTab) {
            case FRAGMENT_SINGLE_WAY:
            case FRAGMENT_RENT_HOURLY:
                super.onBackPressed();
                break;

            case FRAGMENT_CAR_LOCATION_MAP:
                if (mEntrance != null) {
                    switch (mEntrance) {
                        case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                            mCurrentShowFragmentTab = FRAGMENT_RENT_HOURLY;
                            break;
                        case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
                        case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
                            mCurrentShowFragmentTab = FRAGMENT_SINGLE_WAY;
                            break;
                    }
                }
                showOneFragmentOnBackPressed(mCurrentShowFragmentTab);
                break;
            case FRAGMENT_SELECT_CITY: {
                mCurrentShowFragmentTab = FRAGMENT_CAR_LOCATION_MAP;
                OfficialCarLocationMapFragment locationMapFragment =
                        (OfficialCarLocationMapFragment) getSupportFragmentManager()
                                .findFragmentByTag(mCurrentShowFragmentTab);
                locationMapFragment.setFromPOISearchFragment(true);
                showOneFragmentOnBackPressed(mCurrentShowFragmentTab);
                break;
            }

            case FRAGMENT_CAR_POI_SEARCH:
                mCurrentShowFragmentTab = FRAGMENT_CAR_LOCATION_MAP;
                OfficialCarLocationMapFragment locationMapFragment =
                        (OfficialCarLocationMapFragment) getSupportFragmentManager()
                                .findFragmentByTag(mCurrentShowFragmentTab);
                locationMapFragment.setFromPOISearchFragment(true);
                showOneFragmentOnBackPressed(mCurrentShowFragmentTab);
                break;

            default:
                super.onBackPressed();
                break;
        }

    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }

    @Override
    protected void onHeaderCheckedChange(RadioGroup group, int checkedId) {
        if (checkedId > 0) {
            switch (checkedId) {
                case R.id.rb_include_header_single_way:
                    displayOneFragment(
                            FRAGMENT_SINGLE_WAY,
                            CarLocationMapEntrance.DEFAULT);
                    break;
                case R.id.rb_include_header_rent_hourly:
                    displayOneFragment(
                            FRAGMENT_RENT_HOURLY,
                            CarLocationMapEntrance.DEFAULT);
                    break;
            }
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_official_car_container;
    }

    @Override
    protected void initializeInjector() {

    }


    public String getUserSelectCity() {
        return mUserSelectCity;
    }

    public void setUserSelectCity(String userSelectCity) {
        mUserSelectCity = userSelectCity;
    }
}
